/*
 * SoC audio driver for loongson1
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/device.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>

#include "ls1x-i2s.h"

/*-------------------------  I2S PART  ---------------------------*/
#if defined(CONFIG_SND_LS1X_SOC_I2S)
static int ls1x_i2s_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
	int ret;

	/* codec is bitclock and lrclk master */
	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
			SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		goto out;

	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
			SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		goto out;

	ret = 0;
out:
	return ret;
}

static int ls1x_i2s_hw_params(struct snd_pcm_substream *substream, 
					struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
	unsigned int __maybe_unused mclk_freq;
	int ret;

#if defined(CONFIG_LOONGSON_2K1000)
	mclk_freq = 256 * params_rate(params);
	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_freq, SND_SOC_CLOCK_OUT);
#else
	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, 34000000, SND_SOC_CLOCK_OUT);
	// ret = snd_soc_dai_set_sysclk(cpu_dai, 0, 22579600, SND_SOC_CLOCK_OUT);
#endif
	return ret;
}

#ifdef CONFIG_SND_SOC_ES8328
static struct snd_soc_ops ls1x_i2s_es8328_ops = {
	.startup	= ls1x_i2s_startup,
	.hw_params	= ls1x_i2s_hw_params,
};

SND_SOC_DAILINK_DEFS(es8328,
	DAILINK_COMP_ARRAY(COMP_CPU("ls1x-i2s")),
	DAILINK_COMP_ARRAY(COMP_CODEC("es8328.0-0010", "es8328-hifi-analog")),
	DAILINK_COMP_ARRAY(COMP_PLATFORM("loongson1-pcm-audio")));

static struct snd_soc_dai_link es8328_i2s_dai = {
	.name = "es8328",
	.stream_name = "ls1x<->i2s",
	.ops = &ls1x_i2s_es8328_ops,
	SND_SOC_DAILINK_REG(es8328),
};

static struct snd_soc_card es8328_i2s_machine = {
	.name = "LS1X_I2S",
	.dai_link = &es8328_i2s_dai,
	.num_links = 1,
};
#endif
#ifdef CONFIG_SND_SOC_ES8316
static struct snd_soc_ops ls1x_i2s_es8316_ops = {
	.startup	= ls1x_i2s_startup,
	.hw_params	= ls1x_i2s_hw_params,
};

SND_SOC_DAILINK_DEFS(es8316,
	DAILINK_COMP_ARRAY(COMP_CPU("ls1x-i2s")),
	DAILINK_COMP_ARRAY(COMP_CODEC("es8316.1-0011", "ES8316 HiFi")),
	DAILINK_COMP_ARRAY(COMP_PLATFORM("bus@10000000:loongson1-pcm-audio")));

static struct snd_soc_dai_link es8316_i2s_dai = {
	.name = "es8316",
	.stream_name = "ls1x<->i2s",
	.ops = &ls1x_i2s_es8316_ops,
	SND_SOC_DAILINK_REG(es8316),
};

static struct snd_soc_card es8316_i2s_machine = {
	.name = "LS1X_I2S",
	.dai_link = &es8316_i2s_dai,
	.num_links = 1,
};
#endif
#ifdef CONFIG_SND_SOC_ES8336
static struct snd_soc_ops ls1x_i2s_es8336_ops = {
	.startup	= ls1x_i2s_startup,
	.hw_params	= ls1x_i2s_hw_params,
};

SND_SOC_DAILINK_DEFS(es8336,
	DAILINK_COMP_ARRAY(COMP_CPU("ls1x-i2s")),
	DAILINK_COMP_ARRAY(COMP_CODEC("es8336.1-0011", "ES8336 HiFi")),
	DAILINK_COMP_ARRAY(COMP_PLATFORM("bus@10000000:loongson1-pcm-audio")));

static struct snd_soc_dai_link es8336_i2s_dai = {
	.name = "es8336",
	.stream_name = "ls1x<->i2s",
	.ops = &ls1x_i2s_es8336_ops,
	SND_SOC_DAILINK_REG(es8336),
};

static struct snd_soc_card es8336_i2s_machine = {
	.name = "LS1X_I2S",
	.dai_link = &es8336_i2s_dai,
	.num_links = 1,
};
#endif

#ifdef CONFIG_SND_SOC_ES7210
static struct snd_soc_ops ls1x_i2s_es7210_ops = {
	.startup	= ls1x_i2s_startup,
	.hw_params	= ls1x_i2s_hw_params,
};

SND_SOC_DAILINK_DEFS(es7210,
	DAILINK_COMP_ARRAY(COMP_CPU("ls1x-i2s")),
	DAILINK_COMP_ARRAY(COMP_CODEC("es7210.0-0040", "ES7210 4CH ADC 0")),
	DAILINK_COMP_ARRAY(COMP_PLATFORM("bus@10000000:loongson1-pcm-audio")));

static struct snd_soc_dai_link es7210_i2s_dai = {
	.name = "es7210",
	.stream_name = "ls1x<->i2s",
	.ops = &ls1x_i2s_es7210_ops,
	SND_SOC_DAILINK_REG(es7210),
};

static struct snd_soc_card es7210_i2s_machine = {
	.name = "LS1X_I2S",
	.dai_link = &es7210_i2s_dai,
	.num_links = 1,
};
#endif
#endif

/*-------------------------  AC97 PART  ---------------------------*/
#if defined(CONFIG_SND_LS1X_SOC_AC97)
SND_SOC_DAILINK_DEFS(ac97,
	DAILINK_COMP_ARRAY(COMP_CPU("ls1x-ac97")),
	DAILINK_COMP_ARRAY(COMP_CODEC("ac97-codec", "ac97-hifi")),
	DAILINK_COMP_ARRAY(COMP_PLATFORM("loongson1-pcm-audio")));

static struct snd_soc_dai_link ls1x_ac97_dai = {
	.name		= "AC97",
	.stream_name	= "AC97 HiFi",
	SND_SOC_DAILINK_REG(ac97),
};

static struct snd_soc_card ls1x_ac97_machine = {
	.name		= "LS1X_AC97",
	.owner = THIS_MODULE,
	.dai_link	= &ls1x_ac97_dai,
	.num_links	= 1,
};

static struct platform_device *ls1x_snd_ac97_device;
#endif

/*-------------------------  COMMON PART  ---------------------------*/

static int ls1x_probe(struct platform_device *pdev)
{
	struct snd_soc_card *card;
	int ret;

#if defined(CONFIG_SND_LS1X_SOC_I2S)
	#ifdef CONFIG_SND_SOC_ES8328
	card = &es8328_i2s_machine;
	#endif
	#ifdef CONFIG_SND_SOC_ES8316
	card = &es8316_i2s_machine;
	#endif
	#ifdef CONFIG_SND_SOC_ES8336
	card = &es8336_i2s_machine;
	#endif
	#ifdef CONFIG_SND_SOC_ES7210
	card = &es7210_i2s_machine;
	#endif
	#if defined(CONFIG_OF)
	#if defined(CONFIG_MACH_LOONGSON32)
	card->dai_link->cpus->dai_name = "1fe60000.ls1x-i2s";
	#elif defined(CONFIG_LOONGSON_2K1000)
	#if defined(CONFIG_MIPS)
	card->dai_link->cpus->dai_name = "1fe0d000.ls1x-i2s";
	#else
	card->dai_link->cpus->dai_name = "1fe2d000.ls1x-i2s";
	#endif
	#endif
	#endif
#elif defined(CONFIG_SND_LS1X_SOC_AC97)
	ls1x_snd_ac97_device = platform_device_register_simple("ac97-codec",
								 -1, NULL, 0);
	if (IS_ERR(ls1x_snd_ac97_device))
		return PTR_ERR(ls1x_snd_ac97_device);

	card = &ls1x_ac97_machine;
#endif

	card->dev = &pdev->dev;

	ret = snd_soc_register_card(card);
	if (ret) {
		dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
			ret);
#if defined(CONFIG_SND_LS1X_SOC_AC97)
		platform_device_unregister(ls1x_snd_ac97_device);
#endif
	}

	return ret;
}

static int ls1x_remove(struct platform_device *pdev)
{
	struct snd_soc_card *card = platform_get_drvdata(pdev);

	snd_soc_unregister_card(card);
#if defined(CONFIG_SND_LS1X_SOC_AC97)
	platform_device_unregister(ls1x_snd_ac97_device);
#endif

	return 0;
}

static struct platform_driver ls1x_driver = {
	.driver		= {
		.name	= "ls1x-audio",
		.owner	= THIS_MODULE,
	},
	.probe		= ls1x_probe,
	.remove		= ls1x_remove,
};

module_platform_driver(ls1x_driver);

/* Module information */
MODULE_AUTHOR("Tang Haifeng <tanghaifeng-gz@loongson.cn>");
MODULE_DESCRIPTION("ALSA SoC ls1x board Audio support");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:ls1x-audio");
